home *** CD-ROM | disk | FTP | other *** search
- Path: news.voicenet.com!news
- From: kobak@voicenet.com (Peter Kobak)
- Newsgroups: comp.lang.c++
- Subject: Re: Overload of operator=
- Date: 17 Jan 1996 19:13:00 GMT
- Organization: Voicenet - Internet Access - (215)674-9290
- Message-ID: <4djhns$h6s@news.voicenet.com>
- NNTP-Posting-Host: philly227.voicenet.com
- X-Newsreader: NeoLogic News for OS/2 [version: 4.2]
-
- In message <30FBCAA1.34A7@novell.com> - Greg Johnson <gregjo@novell.com>
- writes:
- :>
- :>I want to overload the assignment operator, for some debugging purposes.
- :>But after I overload the operator, somehow, I need to perform the
- :>default assignment. How do I do that if I don't know the exact size
- :>of the source or destination?
- :>How do I avoid recursion and perform the default behavior?
-
- You assign the components of the object, not the binary representation.
-
- :>For example:
- :>
- :>class BaseObj {
- :>public:
- :> long x;
- :> BaseObj& operator=(BaseObj&);
- :> BaseObj(void) : x(0) {};
- :>};
- :>
- :>BaseObj& BaseObj::operator=(BaseObj & ptr)
- :>{
- :> *this = ptr; // this will recursively call operator=
- x = ptr.x; // ** do this instead **
- :> return *this;
- :>}
- :>
- :>void foo()
- :>{
- :> BaseObj bo1;
- :> BaseObj bo2;
- :>
- :> bo1.x = 99;
- :> bo2 = bo1;
- :>}
-
- ================
- Peter Kobak
- kobak@voicenet.com
-
-
-